package com.chaodev.base.controller;

import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.Point;
import com.chaodev.base.model.PointModel;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/point")
@Api(value = "")
public class PointsController {
    @Autowired
    private RedisDao redisDao;

    @ApiOperation(value = "坐标格式化", notes = "坐标格式化,除以100000返回")
    @PostMapping(value = {"/pointParse"})
    public Result pointParse(@RequestBody PointModel pointModel, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            List<Point> list = new ArrayList<>();
            Point newPoint = new Point();
            List<Point> pointList = pointModel.getPointList();
            for (int i = 0; i < pointList.size(); i++) {
                Point point = pointList.get(i);
                Double latitude1 = point == null ? null : point.getLatitude() / 100000;
                Double longitude1 = point == null ? null : point.getLongitude() / 100000;
                newPoint.setLatitude(latitude1);
                newPoint.setLongitude(longitude1);
                list.add(newPoint);
            }
            result.setCode(200);
            result.setMsg("操作成功");
            result.setData(list);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
        }
        return result;
    }
}
